from django.db import models
from django.utils.translation import ugettext_lazy as _

PRES = (
	('A', 'Apress'),
	('B', "O'Reilly"),
	('C', 'Others'),
)

class Tag(models.Model):
	'''Tags collection'''
	name = models.CharField(_('tag name'), max_length=300)
	
	def __unicode__(self):
		return self.name

class Book(models.Model):
	'''Basic information of a book'''
	title = models.CharField(_('title'), max_length=300)
	author = models.CharField(_('author'), max_length=100)
	press = models.CharField(_('press'), max_length=1, choices=PRES, blank=True)
	intro = models.TextField(_('introduction'), blank=True)
	pages = models.IntegerField(_('number of pages'), blank=True)
	pub_date = models.DateField(_('published date'), blank=True)
	isbn = models.CharField(_('ISBN'), max_length=20, blank=True)
	tags = models.ManyToManyField(Tag, through='Tagging')
	num_copy = models.IntegerField(_('number of copies'), default=1)

	def __unicode__(self):
		return self.title

class Tagging(models.Model):
	'''intermediary for many-to-many relations''' 
	tag = models.ForeignKey(Tag)
	book = models.ForeignKey(Book)

	def __unicode__(self):
		return self.book.title + 'is tagged by' + self.tag.name
